package com.youi.web;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.Groupleaderbank;
import com.youi.entity.vo.GroupleadserbankVo;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.service.GroupleaderbankService;
import com.youi.service.GroupleadserbankVoService;
import com.youi.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 团长提现信息控制层
* @author Administrator
*/
@Controller
public class GroupleaderbankController  extends HttpRequestManage{

		@Autowired
		private GroupleaderbankService groupleaderbankService;
		@Autowired
		private GroupleadserbankVoService groupleadserbankVoService;

		/**
		 *添加或编辑团长提现信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditGroupleaderbank")
		public String addOrEditGroupleaderbank(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Groupleaderbank groupleaderbank=null;
			if (id!=null) {
				groupleaderbank=groupleaderbankService.get(id);
			}else {
				groupleaderbank=new Groupleaderbank();
				groupleaderbank.setDelStatus(DelStatus.NODEL);
				groupleaderbank.setGmtCreated(new Date());
			}
			Long leaderId=optLong("leaderId",request);
			if (leaderId!=null){
				groupleaderbank.setLeaderId(leaderId);
			}
			String realName=optString("realName",request);
			if (realName!=null){
				groupleaderbank.setRealName(realName);
			}
			String idCard=optString("idCard",request);
			if (idCard!=null){
				groupleaderbank.setIdCard(idCard);
			}
			Integer status=optInteger("status",request);
			if (status!=null){
				groupleaderbank.setStatus(status);
			}
			String idCardFront=optString("idCardFront",request);
			if (idCardFront!=null){
				groupleaderbank.setIdCardFront(idCardFront);
			}
			String idCareBehind=optString("idCareBehind",request);
			if (idCareBehind!=null){
				groupleaderbank.setIdCareBehind(idCareBehind);
			}
			String handInCard=optString("handInCard",request);
			if (handInCard!=null){
				groupleaderbank.setHandInCard(handInCard);
			}
			String bank=optString("bank",request);
			if (bank!=null){
				groupleaderbank.setBank(bank);
			}
			String bankCode=optString("bankCode",request);
			if (bankCode!=null){
				groupleaderbank.setBankCode(bankCode);
			}
			String branch=optString("branch",request);
			if (branch!=null){
				groupleaderbank.setBranch(branch);
			}
			String branchCode=optString("branchCode",request);
			if (branchCode!=null){
				groupleaderbank.setBranchCode(branchCode);
			}
			String bankCardNo=optString("bankCardNo",request);
			if (bankCardNo!=null){
				groupleaderbank.setBankCardNo(bankCardNo);
			}
			String province=optString("province",request);
			if (province!=null){
				groupleaderbank.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				groupleaderbank.setCity(city);
			}
			String companyCode=optString("companyCode",request);
			if (companyCode!=null){
				groupleaderbank.setCompanyCode(companyCode);
			}
			String bankFront=optString("bankFront",request);
			if (bankFront!=null){
				groupleaderbank.setBankFront(bankFront);
			}
			String bankBehind=optString("bankBehind",request);
			if (bankBehind!=null){
				groupleaderbank.setBankBehind(bankBehind);
			}
			groupleaderbank.setGmtModify(new Date());
			groupleaderbankService.saveOrUpdate(groupleaderbank);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除团长提现信息
		*/
		@ResponseBody
		@RequestMapping("delGroupleaderbankByIds")
		public String delGroupleaderbankByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Groupleaderbank> groupleaderbankList= groupleaderbankService.getByParams(params);
			if (groupleaderbankList==null||groupleaderbankList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Groupleaderbank groupleaderbank : groupleaderbankList) {
				groupleaderbank.setDelStatus(DelStatus.DEL);
			}
			groupleaderbankService.saveOrUpdateAll(groupleaderbankList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-114
		 * 查询团长提现资料列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getGroupleaderbank")
		public String getGroupleaderbank(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			Integer status=optInteger(Common.STATUS, request);
			String name=optString("name",request);
			String start=optString("start",request);
			String end=optString("end",request);
			String auditStart=optString("auditStart",request);
			String auditEnd=optString("auditEnd",request);
			String leaderStr=optString("leaderStr", request);
			String store=optString("store", request);

			Map<String, Object> params = new HashMap();
			params.put("pageIndex",(pageIndex-1)*pageNum);
			params.put("pageNum",pageNum);
			params.put(Common.STATUS,status);
			params.put("name",name);
			params.put("start",start);
			params.put("end",end);
			params.put("auditStart",auditStart);
			params.put("auditEnd",auditEnd);
			params.put("leaderStr",leaderStr);
			params.put("store",store);

			List<GroupleadserbankVo> groupleadserbankVoList=groupleadserbankVoService.getGroupleadserbankVoList(params);
			if (groupleadserbankVoList==null||groupleadserbankVoList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=groupleadserbankVoService.getGroupleadserbankVoCount(status, name, leaderStr, store, start, end, auditStart, auditEnd);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, groupleadserbankVoList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-115
		 * 查询团长提现信息详情
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getGroupleaderbankById")
		public String getGroupleaderbankById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			GroupleadserbankVo groupleadserbankVo=groupleadserbankVoService.getGroupleadserbankVo(id);
			if (groupleadserbankVo==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",groupleadserbankVo);//jsonObject	json对象
		}
		
		/**
		 * I-116
		 * 审核团长提现资料
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/auditGroupleaderbank")
		public String auditGroupleaderbank(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			Integer status=getInteger(Common.STATUS, request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Groupleaderbank> groupleaderbankList= groupleaderbankService.getByParams(params);
			if (groupleaderbankList==null||groupleaderbankList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Groupleaderbank groupleaderbank : groupleaderbankList) {
				groupleaderbank.setStatus(status);
			}
			groupleaderbankService.saveOrUpdateAll(groupleaderbankList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");//jsonObject	json对象
		}
		
		/**
		 * I-117
		 * 导出团长提现资料excel
		*/
		@ResponseBody
		@RequestMapping("toExcelGroupleaderbank")
		public String toExcelGroupleaderbank(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{			
//			Integer status=optInteger(Common.STATUS, request);
//			String name=optString("name",request);
//			String start=optString("start",request);
//			String end=optString("end",request);
//			String auditStart=optString("auditStart",request);
//			String auditEnd=optString("auditEnd",request);
//			String phone=optString("phone", request);
//			String store=optString("store", request);
//			List<GroupleadserbankVo> groupleadserbankVoList=groupleadserbankVoService.getGroupleadserbankVoList(status, name, phone, store, start, end, auditStart, auditEnd, null,null);
//			if (groupleadserbankVoList==null||groupleadserbankVoList.size()==0) {
//				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
//			}
//			groupleadserbankVoService.toExcelGroupleaderbank(request, groupleadserbankVoList);
			modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
			modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");	//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}


		/**
		 * I-117
		 * 导出团长提现资料excel--new
		 */
		@ResponseBody
		@RequestMapping(value = "sysUserGetInfo/exportGroupLeaderBand", produces = "application/json")
		public void exportGroupLeaderBand(@RequestBody Map<String, Object> param, HttpServletRequest request, HttpServletResponse response) throws BusinessException, ParseException {
//			String  statusTxt = String.valueOf(param.get(Common.STATUS)).equals("")?null:String.valueOf(param.get(Common.STATUS));
//			Integer status = null;
//			if(statusTxt!=null){
//				status = Integer.parseInt(String.valueOf(param.get(Common.STATUS)).equals("")?null:String.valueOf(param.get(Common.STATUS)));
//			}
//			String name= String.valueOf(param.get("name"));
//			String start=String.valueOf(param.get("start"));
//			String end=String.valueOf(param.get("end"));
//			String auditStart=String.valueOf(param.get("auditStart"));
//			String auditEnd=String.valueOf(param.get("auditEnd"));
//			String phone=String.valueOf(param.get("phone"));
//			String store=String.valueOf(param.get("store"));
			param.put("pageIndex",null);
			param.put("pageNum",null);
			System.out.println(param.toString());
			List<GroupleadserbankVo> groupleadserbankVoList=groupleadserbankVoService.getGroupleadserbankVoList(param);

			if (CollectionUtils.isEmpty(groupleadserbankVoList)) {
				throw new BusinessException(-1, "没有要导出的数据");
			}

			try {
				String fileName = "佳选官提现列表" + DateUtil.simpyMdHHmmss.format(new Date());
				ExportParams exportParams = new ExportParams(null, "佳选官提现列表", ExcelType.XSSF);
				exportParams.setStyle(ExcelStyleUtil.class);

				EasyPoiUtil.exportExcel(fileName, exportParams, GroupleadserbankVo.class, groupleadserbankVoList, request, response);
			} catch (Exception ex) {
				throw new BusinessException(-1, "导出数据失败");
			}
		}



		/**
		 * 查询所有团长提现信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllGroupleaderbank")
		public String getAllGroupleaderbank(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Groupleaderbank> groupleaderbankList= groupleaderbankService.getByParams(null);
			if (groupleaderbankList==null||groupleaderbankList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",groupleaderbankList);//jsonObjectList	json对象集合
		}
}
